package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.atguigu.spzx.manager.mapper.*;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductSaveDTO;
import com.atguigu.spzx.model.dto.product.ProductSearchDTO;
import com.atguigu.spzx.model.entity.product.*;
import com.atguigu.spzx.model.vo.product.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LT
 * @create 2024-03-18-20:59
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Resource
    ProductDetailsMapper productDetailsMapper;

    @Resource
    ProductSkuMapper productSkuMapper;

    @Resource
    BrandMapper brandMapper;

    @Resource
    CategoryMapper categoryMapper;


    @Override
    public PageInfo<ProductVo> list(Integer pageNum, Integer pageSize, ProductSearchDTO productSearchDTO) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductVo> list = productMapper.list(productSearchDTO);
        return new PageInfo<>(list);
    }

    @Override
    public void deleteById(Long id) {
        productDetailsMapper.deleteByProductId(id);
        productSkuMapper.deleteByProductId(id);
        productMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveProduct(ProductSaveDTO productSaveDTO) {
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDTO,product);
        product.setAuditStatus(0);
        product.setStatus(0);
        product.setAuditMessage("...");
        productMapper.save(product);

        Long productId = product.getId();
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(productId);
        productDetails.setId(productId);
        productDetails.setImageUrls(productSaveDTO.getDetailsImageUrls());
        productDetailsMapper.saveDetails(productDetails);

        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        List<ProductSku> productSkuList = productSaveDTO.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSku.setProductId(productId);
            productSku.setSkuCode(snowflake.nextIdStr());
            productSku.setSkuName(product.getName()+" "+productSku.getSkuSpec());
            productSku.setStatus(0);
            productSku.setSaleNum(0);
        });
        productSkuMapper.saveBatch(productSkuList);
    }

    @Override
    public ProductSaveDTO getById(Long id) {
        Product product = productMapper.getById(id);
        ProductDetails productDetails = productDetailsMapper.selectById(id);
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);

        ProductSaveDTO productSaveDTO = new ProductSaveDTO();
        BeanUtils.copyProperties(product,productSaveDTO);
        productSaveDTO.setDetailsImageUrls(productDetails.getImageUrls());
        productSaveDTO.setProductSkuList(productSkuList);

        Brand brand = brandMapper.getById(product.getBrandId());
        Category category1 = categoryMapper.selectById(product.getCategory1Id());
        Category category2 = categoryMapper.selectById(product.getCategory2Id());
        Category category3 = categoryMapper.selectById(product.getCategory3Id());

        productSaveDTO.getParams().put("brandName" , brand.getName());
        productSaveDTO.getParams().put("category1Name" , category1.getName());
        productSaveDTO.getParams().put("category2Name" , category2.getName());
        productSaveDTO.getParams().put("category3Name" , category3.getName());
        return productSaveDTO;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(ProductSaveDTO productSaveDTO) {
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDTO,product);
        product.setAuditStatus(0);
        product.setStatus(0);
        productMapper.updateById(product);

        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(productSaveDTO.getId());
        productDetails.setImageUrls(productSaveDTO.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);

        List<ProductSku> productSkuList = productSaveDTO.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSku.setSkuName(product.getName()+" "+productSku.getSkuName());
            productSkuMapper.updateById(productSku);
        });
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        productMapper.updateAuditStatus(id,auditStatus);

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        productMapper.updateStatus(id,status);
        productSkuMapper.updateStatus(id,status);
    }
}
